<template>
<template v-if='visible'>
    <Teleport to="body">
        <div class="sporty-dialog-overlay" @click="onClickOverlay"></div>
        <div class="sporty-dialog-wrapper">
            <div class="sporty-dialog">
                <header>
                    <slot name="title" /><span @click="close" class="sporty-dialog-close"></span>
                </header>
                <main>
                    <slot name="content" />
                </main>
                <footer>
                    <Button class="sporty-dialog-sure" level="main" @click="ok">OK</Button>
                    <Button class="sporty-dialog-reject" @click="cancel">Cancel</Button>
                </footer>
            </div>
        </div>
    </Teleport>
</template>
</template>

<script lang="ts">
import Button from './Button.vue';
export default {
    components: {
        Button
    },
    props: {
        visible: {
            type: Boolean,
            defalut: false,
        },
        closeOnClickOverlay: {
            type: Boolean,
            default: true
        },
        ok: {
            type: Function,
        },
        cancel: {
            type: Function,
        },
        // title: {
        //     type: String,
        //     default: '小标题',
        // }
    },
    setup(props, context) {
        //close需要触发事件则要引入props，context
        const close = () => {
            context.emit('update:visible', false);
        }
        //控制遮罩层关闭是否为可用，如果为真则是直接调用关闭函数
        const onClickOverlay = () => {
            console.log(props.closeOnClickOverlay);
            if (props.closeOnClickOverlay)
                close();
        }
        const ok = () => {
            //条件为props中ok存在且不为false，是if(props.ok && props.ok!==false)的简写方法，重点是.?()的使用
            //同样是满足条件可关闭才可调用close函数
            console.log(props.ok);
            if (props.ok?.() !== false)
                close();
        }
        const cancel = () => {
            //仍要向上冒泡cancel让上下文知道传过cancel值后才调用close实现关闭函数
            //context.emit('cancel');
            //表示如果props.cancel存在，则会执行props.cancel()函数
            props.cancel?.();
            close();
        }
        return {
            close,
            onClickOverlay,
            ok,
            cancel
        };
    }
}
</script>

<style lang="scss">
$radius: 4px;
$border-color: #d9d9d9;

.sporty-dialog {
    background: white;
    border-radius: $radius;
    box-shadow: 0 0 3px fade_out(black, 0.5);
    min-width: 15em;
    max-width: 90%;

    &-overlay {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: fade_out(black, 0.5);
        z-index: 10;
    }

    &-wrapper {
        //为何设置一层wrapper，用意何在？
        //使得弹窗居中？
        position: fixed;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        z-index: 11;
        color: #5b5670;
    }

    >header {
        padding: 12px 16px;
        border-bottom: 1px solid $border-color;
        display: flex;
        align-items: center;
        justify-content: space-between;
        font-size: 20px;
    }

    >main {
        padding: 12px 16px;
    }

    >footer {
        border-top: 1px solid $border-color;
        padding: 12px 16px;
        text-align: right;
    }

    &-close {
        position: relative;
        display: inline-block;
        width: 16px;
        height: 16px;
        cursor: pointer;

        &::before,
        &::after {
            content: '';
            position: absolute;
            height: 1px;
            background: black;
            width: 100%;
            top: 50%;
            left: 50%;
        }

        &::before {
            transform: translate(-50%, -50%) rotate(-45deg);
        }

        &::after {
            transform: translate(-50%, -50%) rotate(45deg);
        }
    }
}
</style>
